{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "9b248a77-984d-4b13-8967-85358b588a53",
   "metadata": {},
   "source": [
    "## 小数对象"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "dd36f6a0-8b2a-45b0-9c65-bf4c1cbe09b1",
   "metadata": {},
   "outputs": [],
   "source": [
    "from decimal import Decimal"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "b14e63c0-0ebf-496f-a2c0-418be667f6c1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.1"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "0.1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d3894ad9-a1f0-4858-8036-e44913ac3447",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "b5505aa6-5c42-438a-a61b-8d35f05ac3e1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Decimal('0.1000000000000000055511151231257827021181583404541015625')"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Decimal(0.1) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "53283bb8-84dd-43b5-af1e-7bde634320e4",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "49967723-5b98-4ef2-8b7d-ecd62e7600aa",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Decimal('0.1')"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Decimal('0.1') "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "f8f9ee5f-b40f-4e07-af86-65c4b03db19b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Decimal('0.3')"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Decimal(\"0.1\") + Decimal(\"0.2\") "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "eddfefa2-0d20-4d28-b811-2f525dd4d77e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Decimal(\"0.1\") + Decimal(\"0.2\") == Decimal(\"0.3\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ebb7a73b-1178-49bb-b542-3981cac8100b",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "1a5c0417-d848-4fe0-a4b5-fc9cb2d496cf",
   "metadata": {},
   "outputs": [],
   "source": [
    "from decimal import getcontext\n",
    "getcontext().prec = 4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "40a09615-07bc-4d7e-9d94-63330adc1b52",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Decimal('0.1429')"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Decimal(1) / Decimal(7)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2058ff1f-c76d-41ad-af46-245cc762472f",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "14daa8a6-eb16-42c6-a2bf-aac93a4f2111",
   "metadata": {},
   "source": [
    "## 分数对象"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "d3c5e47d-0cf0-48b1-97e6-484376ae1880",
   "metadata": {},
   "outputs": [],
   "source": [
    "from fractions import Fraction"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "9e157f69-facc-42ef-9202-af13e8d49e04",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Fraction(2, 3)"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Fraction(2, 3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "2f595d33-2bed-46e3-ac2a-737c65b1abbc",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Fraction(7, 12)"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Fraction(1, 3) +  Fraction(1, 4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "67c6bb2a-1e23-47ae-be52-ab2e2b3ae2a7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "7 12\n"
     ]
    }
   ],
   "source": [
    "a, b = Fraction(1, 3), Fraction(1, 4)\n",
    "c = a + b\n",
    "\n",
    "print(c.numerator, c.denominator)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "5ee0b2de-8aba-40a0-ba36-6ed609b99c05",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.5833333333333334"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "float(c)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "94646284-4ddf-4528-b8eb-d095aa3689de",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Decimal('0.5833')"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from decimal import Decimal, getcontext\n",
    "getcontext().prec = 4\n",
    "\n",
    "Decimal(c.numerator) / Decimal(c.denominator)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "d022ed30-34be-4f53-a1bc-81e429af4b50",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.5833333333333334"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "float(c)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
